package com.${ltdAbbr}.xiao.${module}.task.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    @Qualifier("loginCheckInterceptor")
    private HandlerInterceptor loginInterceptor;
    
    /**
     * 注册自定义 类型转换器 [String --> Number] 当类型转换失败返回null
     * @param defaultConversionService
     */
    /*@Autowired
    @Qualifier("mvcConversionService")
    public void setDefaultConversionService(ConfigurableConversionService defaultConversionService){
        defaultConversionService.removeConvertible(String.class, Number.class);
        defaultConversionService.addConverterFactory(new HrStringToNumberConverterFactory());
        defaultConversionService.addConverterFactory(new StringToDateConverterFactory());
    }*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(loginInterceptor);
        loginInterceptor = null;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
            .serializationInclusion(JsonInclude.Include.NON_NULL)
            .failOnUnknownProperties(false)
            .failOnEmptyBeans(false)
            .featuresToEnable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS)
            .featuresToDisable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build();
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
        converters.add(new StringHttpMessageConverter());
    }

}
